package com.backend.modules.notification.service.impl;

import com.backend.common.exception.BusinessException;
import com.backend.common.utils.SecurityUtils;
import com.backend.modules.notification.entity.Notification;
import com.backend.modules.notification.enums.NotificationType;
import com.backend.modules.notification.repository.NotificationRepository;
import com.backend.modules.notification.service.NotificationService;
import com.backend.modules.system.entity.SysUser;
import com.backend.modules.task.entity.Task;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class NotificationServiceImpl implements NotificationService {

    private final NotificationRepository notificationRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void send(Long receiverId, String title, String content, NotificationType type, Long relatedId) {
        Notification notification = new Notification();
        notification.setReceiverId(receiverId);
        notification.setTitle(title);
        notification.setContent(content);
        notification.setType(type.name());
        notification.setRead(false);
        notification.setCreateTime(LocalDateTime.now());
        notification.setRelatedId(relatedId);
        
        notificationRepository.save(notification);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendDeadlineReminder(SysUser student, Task task) {
        String title = "任务截止提醒";
        String content = String.format("您的任务\"%s\"即将截止，请及时提交！", task.getName());
        
        send(student.getId(), title, content, NotificationType.DEADLINE_REMINDER, task.getId());
    }
} 